package von.seiji.cn.test;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.junit.Test;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class LambdaT {

    @Test
    public void t1() {
        Object 哈哈哈 = Lists.newArrayList().stream().filter(Objects::nonNull).findFirst().orElse("哈哈哈");
        System.out.println("哈哈哈 = " + 哈哈哈);
        Object 哈哈哈111 = Lists.newArrayList().stream().filter(Objects::nonNull).findAny().map(item -> "UUUU").get();
        System.out.println("哈哈哈111 = " + 哈哈哈111);
    }

    @Test
    public void t2() {
        Map<String, ArrayList<Integer>> a = Maps.asMap(Sets.newHashSet("a"), input -> Lists.newArrayList(1, 2, 3, 4, 5));
        Map<String, ArrayList<Integer>> a1 = Maps.asMap(Sets.newHashSet("b"), input -> Lists.newArrayList(1, 2, 3, 4, 5));
        Map<String, ArrayList<Integer>> stringArrayListMap = Stream.of(a, a1).max(Comparator.comparingInt(item -> {
                ArrayList<Integer> integers = item.entrySet().stream().findFirst().map(Map.Entry::getValue).orElse(null);
                int sum = integers.stream().mapToInt(item1 -> item1).sum();
                System.out.println("sum = " + sum);
                System.out.println(integers.stream().reduce(Integer::sum).orElse(-999));
                return sum;
        })).get();
        System.out.println("stringArrayListMap = " + stringArrayListMap);

      /*  int i = Stream.of(4, 23, 43, 76, 7, 98).mapToInt(item -> item).max().orElse(0);
        System.out.println("i = " + i);*/

        System.out.println("=============");


    }

    @Test
    public void testMap() {

        Integer integer = Stream.of(1, 4, 5, 6, 8, 9).reduce(100, (item1, item2) -> {
            System.out.println("item1 = " + item1);
            System.out.println("item2 = " + item2);
            return item1 + item2;
        });
        System.out.println("integer = " + integer);

        Map<String, ArrayList<String>> asMap = Maps.<String, ArrayList<String>>asMap(Sets.newHashSet("a", "b"), key -> Lists.newArrayList(key + "666"));
        Map<String, ArrayList<String>> asMap1 = Maps.<String, ArrayList<String>>asMap(Sets.newHashSet("a", "b1"), key -> Lists.newArrayList(key + "777"));
        Map<String, ArrayList<String>> c = Maps.asMap(Sets.newHashSet("c"), input -> Lists.newArrayList(input + "999"));

        Map<String, ArrayList<String>> orElse = Stream.of(asMap, asMap1).reduce((item, item2) -> {
            return item.entrySet().stream().flatMap(entry -> {
                return item2.entrySet().stream().collect(Collectors.toMap(entry1 -> {
                    return entry.getKey() + entry1.getKey();
                }, entry1 -> {
                    ArrayList<String> collect = (ArrayList<String>) Stream.of(entry.getValue()
                            , entry1.getValue()).flatMap(List::stream).collect(Collectors.toList());
                    return collect;
                })).entrySet().stream();
            }).collect(Collectors.toMap(Map.Entry::getKey
                    , Map.Entry::getValue));
        }).orElse(c);

        System.out.println("orElse = " + orElse);

        System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++");
        Map<String, ArrayList<String>> collect = Stream.concat(asMap1.entrySet()
                .stream(), asMap.entrySet().stream())
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (k1, k2) -> {
                    System.out.println("k1 = " + k1);
                    System.out.println("k2 = " + k2);
                    return k1;

                }));
        System.out.println("collect = " + collect);
        Map<String, ArrayList<String>> collect1 = Stream.of(asMap, asMap1).flatMap(item -> item.entrySet().stream())
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (k1, k2) -> {
                    System.out.println("k1 = " + k1);
                    System.out.println("k2 = " + k2);
                    return k1;

                }));
        System.out.println("collect1 = " + collect1);
    }

    @Test
    public void teafd() {
        ArrayList<String> strings = Lists.newArrayList(Lists.newArrayList("a", "b"));
        ArrayList<String> strings1 = new ArrayList<>(Lists.newArrayList("111", "222"));



        List<ArrayList<String>> collect = strings.stream().flatMap(item -> strings1.stream().map(item1 -> {
            if (Integer.parseInt(item1) < 200) {
                return null;
            }
            return ((ArrayList<String>) Lists.newArrayList(item, item1).stream().filter(item3 -> item3.length() > 1000).collect(Collectors.toList()));
        })).collect(Collectors.toList());
        System.out.println("collect = " + collect);
        System.out.println(collect.stream().filter(Objects::nonNull).collect(Collectors.toList()));

        List<String> collect1 = strings1.stream().flatMap(item -> {
            if (Integer.parseInt(item) > 200) {
                return Stream.of(item);
            }
            return null;
        }).collect(Collectors.toList());
        System.out.println("collect1 = " + collect1);

        List<Stream<String>> collect2 = strings1.stream().map(item -> {
            if (Integer.parseInt(item) > 200) {
                return Stream.of(item);
            }
            return Stream.<String>empty();
        }).collect(Collectors.toList());
        System.out.println(collect2);

        Stream.of(3333,null, 12).forEach(System.out::println);

    }

    @Test
    public void etlafkd() {
        int a = 9;
        System.out.println(a == 8 ? 1111 : a > 2 ? 9999 : -1111);
    }
}
